﻿/*
	Date:			05/19/2012
	Author:         Fabien Olousouzian
	License:      	Microsoft Public License (Ms-PL)
	Product:        XNA Shader Composer - Project
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Diagnostics;

namespace Shader_Composer
{
    /// <summary>
    /// Draw a simple line
    /// </summary>
    class Line
    {
        #region Enum

        public enum Axe
        {
            X,
            Y,
            Z
        };

        #endregion

        #region Attributes

        private VertexPositionColor[] _oriPosition;
        private VertexPositionColor[] _position;
        private float _rotation;

        #endregion

        #region Methods

        /// <summary>
        /// Ctor by default
        /// </summary>
        public Line()
        {
            _position = new VertexPositionColor[2];
            _oriPosition = new VertexPositionColor[2];
            _rotation = 0f;
        }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="firstPosition"></param>
        /// <param name="secondPosition"></param>
        public Line(Vector3 firstPosition, Vector3 secondPosition)
        {
            _position = new VertexPositionColor[2];
            _position[0] = new VertexPositionColor(firstPosition, Color.Black);
            _position[1] = new VertexPositionColor(secondPosition, Color.Black);

            _oriPosition = new VertexPositionColor[2];
            _oriPosition[0] = new VertexPositionColor(firstPosition, Color.Black);
            _oriPosition[1] = new VertexPositionColor(secondPosition, Color.Black);

            _rotation = 0f;
        }

        /// <summary>
        /// Rotation
        /// </summary>
        /// <param name="axe"></param>
        /// <param name="rotation"></param>
        public void RotationAxisAngle(Axe axe, float rotationRadians)
        {
            if (rotationRadians > 0)
                this.Rotation += rotationRadians;
            else
                this.Rotation += rotationRadians;


            if (axe == Axe.Y)
            {
                _position[1].Position.X = _oriPosition[1].Position.X * (float)Math.Cos(_rotation) - _oriPosition[1].Position.Z * (float)Math.Sin(_rotation);
                _position[1].Position.Z = _oriPosition[1].Position.X * (float)Math.Sin(_rotation) + _oriPosition[1].Position.Z * (float)Math.Cos(_rotation);
            }

            if (axe == Axe.Z)
            {
                _position[1].Position.X = _oriPosition[1].Position.X * (float)Math.Cos(_rotation) - _oriPosition[1].Position.Y * (float)Math.Sin(_rotation);
                _position[1].Position.Y = _oriPosition[1].Position.X * (float)Math.Sin(_rotation) + _oriPosition[1].Position.Y * (float)Math.Cos(_rotation);
            }

            if (axe == Axe.X)
            {
                _position[1].Position.Z = _oriPosition[1].Position.Z * (float)Math.Cos(_rotation) - _oriPosition[1].Position.Y * (float)Math.Sin(_rotation);
                _position[1].Position.Y = _oriPosition[1].Position.Z * (float)Math.Sin(_rotation) + _oriPosition[1].Position.Y * (float)Math.Cos(_rotation);
            }

        }

        /// <summary>
        /// Draw the line
        /// </summary>
        /// <param name="effect"></param>
        /// <param name="graphics"></param>
        public void Draw(BasicEffect effect, GraphicsDevice graphics)
        {
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphics.DrawUserPrimitives(PrimitiveType.LineList, _position, 0, _position.Length / 2);
            }
        }

        #endregion

        #region Getter & Setter

        /// <summary>
        /// Get & set the first position
        /// </summary>
        public VertexPositionColor FirstPosition
        {
            get { return _oriPosition[0]; }
            set { _oriPosition[0] = value; _position[0] = value; }
        }

        /// <summary>
        /// Get & set the second position
        /// </summary>
        public VertexPositionColor SecondPosition
        {
            get { return _oriPosition[1]; }
            set { _oriPosition[1] = value; _position[1] = value; }
        }

        /// <summary>
        /// Get & set the rotation
        /// </summary>
        public float Rotation
        {
            get { return _rotation; }
            set
            {
                if (MathHelper.ToDegrees(_rotation) <= -360)
                    _rotation += MathHelper.ToRadians(360);
                else if (MathHelper.ToDegrees(_rotation) >= 360)
                    _rotation -= MathHelper.ToRadians(360);
                else
                    _rotation = value;
            }
        }


        #endregion
    }
}
